<!-- vue-management-system/src/views/Login.vue -->
<template>
    <div class="login-container">
      <el-card>
        <h2>用户登录</h2>
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginForm.password" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script>
  import { ref } from 'vue'
  import { ElMessage } from 'element-plus'
  import axios from 'axios'
  
  export default {
    name: 'Login',
    setup() {
      const loginForm = ref({
        username: '',
        password: ''
      })
  
      const rules = {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
  
      const handleLogin = async () => {
        try {
          const response = await axios.post('http://localhost:3000/auth/login', loginForm.value)
          ElMessage.success('登录成功')
          // 这里可以将 token 存储到 localStorage 或 Vuex
          localStorage.setItem('access_token', response.data.access_token)
          // 跳转到用户管理页面
          window.location.href = '/user'
        } catch (error) {
          ElMessage.error('登录失败: ' + error.response.data.message)
        }
      }
  
      return {
        loginForm,
        rules,
        handleLogin
      }
    }
  }
  </script>
  
  <style scoped>
  .login-container {
    width: 300px;
    margin: 100px auto;
  }
  </style>